var _set = require("../../../API/set.js"),_util = require("../../../lib/utils/util.js");
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    src:'',
    ocrimg:'',
    fee:'2.00',
    addrInfo:app.globalData.addrInfo,
    school:[],
    schoolId:0,
    postSrc:'',
    uploadImg:'',
    hasImg:0,
    payShow:0,
    deviceType:app.globalData.deviceType
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSchool();
    this.getFee();
    this.setData({
      deviceType:app.globalData.deviceType
    })
  },
  getFee:function(){
    var self = this;
    (0, _set.getFee)().then(function(t) {
        self.setData({
          fee: t.data.pay_price,
        });
    }).catch(function(t) {
      return app.Tips({
          title: t
      });
  });
  },
  getSchool:function(){
    var self = this;
    (0, _set.getSchoolList)().then(function(t) {
        self.setData({
          school: t.data,
        });
    }).catch(function(t) {
      return app.Tips({
          title: t
      });
  });
  },
  uploadImg: function() {
    var a = this;
    _util.uploadImageOne("api/common/uploadImage", function(t) {
        a.setData({
          uploadImg: t.data.url,
          hasImg:1
        });
    });
  },
  changeschool:function(e){
    this.setData({
      schoolId:e.currentTarget.dataset.id
    })
  },
  onShow: function () {
    // console.log(app.globalData.addrInfo)
    this.setData({
      addrInfo:app.globalData.addrInfo
    })
  },
  openCropper:function(){
    var that = this;
    wx.getSystemInfo({
        success (res) {
            that.setData({
                width: res.windowWidth,
                height: res.windowWidth/4*3
            })
        }
      })
    
    wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success (res) {
          const tempFilePaths = res.tempFilePaths;
          if(tempFilePaths){
              that.setData({
                isCropper:1
              })
               //获取到image-cropper对象
               that.cropper = that.selectComponent("#image-cropper");
                const filePath = decodeURIComponent(tempFilePaths); //图片临时地址
                //开始裁剪
                that.setData({
                    src: filePath,
                });
                wx.showLoading({
                title: '加载中'
                })
          }
        }
      })
},
cropperload(e) {
  },
  loadimage(e) {
    wx.hideLoading();
    //重置图片角度、缩放、位置
    this.cropper.imgReset();
  },
  clickcut(e) {
    //点击裁剪框阅览图片
    wx.previewImage({
      current: e.detail.url, // 当前显示图片的http链接
      urls: [e.detail.url] // 需要预览的图片http链接列表
    })
  },
  priveImg:function(e){
    wx.previewImage({
      current: e.currentTarget.dataset.url, // 当前显示图片的http链接
      urls: [e.currentTarget.dataset.url] // 需要预览的图片http链接列表
    })
  },
  submit: function () {
    const _this = this;
    this.cropper.getImg((obj) => {
        // console.log(obj)
        _this.setData({
          postSrc:obj.url,
          isCropper:0,
          haveImg:1
        })
    });
  },
  distinguish:function(){
    const src = this.data.postSrc;
    if(src){
      const _this = this;
      wx.showLoading({
        title: '识别中...',
        mask: true,
      });
      wx.uploadFile({
          url: app.globalData.url + "/api/common/ocr",
          filePath: src,
          name: "image",
          formData: {
              filename: "image"
          },
          header: {
              "Content-Type": "multipart/form-data",
              Authorization: "Bearer " + app.globalData.token
          },
          success: function(a) {
            console.log(JSON.parse(a.data).data.url)
              wx.hideLoading();
              _this.setData({
                  words:JSON.stringify(JSON.parse(a.data).data.words),
              });
          },
          fail: function(a) {
              return app.Tips({
                  title: a.errMsg
              });
          }
      });
    }
  },
  bindTextAreaBlur:function(e){
    this.setData({
      words:e.detail.value
    })
  },
  rotate() {
    //在用户旋转的基础上旋转90°
    this.cropper.setAngle(this.cropper.data.angle += 90);
  },
  end(e) {
    clearInterval(this.data[e.currentTarget.dataset.type]);
  },
  submitTask:function(){
    if(this.data.schoolId == 0){
      return app.Tips({
        title: '请选择学校'
     });
    }
    if(!this.data.hasImg){
      return app.Tips({
        title: '请先选择照片'
     });
    }
    // this.setData({
    //   payShow:1
    // })
    this.payOrder()
  },
  payOrder:function(e){
    console.log(this.data.addrInfo)
    void wx.showLoading({
      title: "支付中..."
    }), (0, _set.submitTask)({
      type:1,
      pay_type:'weixin',
      shop_id:this.data.addrInfo.shop_id,
      school_id:this.data.schoolId,
      from_name:this.data.addrInfo.realname,
      from_address:this.data.addrInfo.address,
      from_phone:this.data.addrInfo.phone,
      to_address:this.data.words,
      pay_price:2,
      to_pic:this.data.uploadImg
    }).then(function(t) {
      wx.hideLoading();
      var e = t.data.status, a = t.data.result.orderId, i = t.data.result.data, r = "/order/pages/order_detail/index?id=" + a; 
      if(t.data.pay_type == "yue"){
          if(e == "SUCCESS"){
                  return wx.hideLoading(),app.Tips({
                      title: t.msg,
                      icon: "success"
                  }, {
                      tab: 5,
                      url: r
                  })
          }else{
              return wx.hideLoading(), app.Tips({
                  title: t.data.message
              }, {
                  tab: 5,
                  url: ""
              });
          }
      }else if(t.data.pay_type == "weixin_pay"){
           wx.requestPayment({
                timeStamp: i.timestamp,
                nonceStr: i.nonceStr,
                package: i.package,
                signType: i.signType,
                paySign: i.paySign,
                success: function(res) {
                    return wx.hideLoading(),app.Tips({
                        title: "支付成功",
                        icon: "success"
                    }, {
                        tab: 5,
                        url: r
                    })
                },
                fail: function(res) {
                  console.log(res)
                  return wx.hideLoading()
                },
            });
      }else{
          if(t.data.status == "SUCCESS"){
              app.Tips({
                  title: t.msg,
                  icon: "success"
              }, {
                  tab: 5,
                  url: r
              })
          }else{
              app.Tips({
                  title: t.msg,
                  icon: "none"
              })
          }
      }
    }).catch(function(t) {
      return app.Tips({
          title: t
      });
  });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})